// 引入业务层
const ProductService = require("../../services/admin/ProductService");

const ProductController = {
  addProduct: async (req, res) => {
    const { title, introduction, detail } = req.body;
    const cover = req.file ? `/productuploads/${req.file.filename}` : "";
    await ProductService.addProduct({
      title,
      introduction,
      detail,
      cover,
      productTime: new Date()
    });

    res.send({
      ActionType: "OK",
    });
  },
  productList: async (req, res)=>{
    let result =  await ProductService.getProductList({_id : req.params.id})
    res.send({
      ActionType: "OK",
      data: result,
    });
  },
  deleteProduct: async (req, res)=>{
    await ProductService.deleteProduct({_id : req.params.id})
    res.send({
      ActionType: "OK",
    });
  },
  updateProduct:async (req, res) => {
    const { _id, title, introduction, detail } = req.body;
    const cover = req.file ? `/productuploads/${req.file.filename}` : "";
    await ProductService.updateProduct({
      _id,
      title,
      introduction,
      detail,
      cover,
      productTime: new Date(),
    });
    res.send({
      ActionType: "OK",
    });
  },
};

module.exports = ProductController;
